package data.join.com.database.bean;

import android.text.TextUtils;

import java.io.File;
import java.util.List;

import data.join.com.database.GreenDao;
import data.join.com.http.GetVideo;
import data.join.com.utils.FileUtils;
import data.join.com.utils.MyLog;

/**
 * Created by wenlong on 16-4-18.
 */
public class VideoDBPref {
    private static String TAG = VideoDBPref.class.getSimpleName();
    private static VideoDBPref mVideoDBPref;
    private VideoBeanDao videoBeanDao;

    private VideoDBPref() {
        videoBeanDao = GreenDao.getInstance().sDaoSession.getVideoBeanDao();
    }

    public static VideoDBPref getInstance() {
        if (mVideoDBPref == null) {
            mVideoDBPref = new VideoDBPref();
        }
        return mVideoDBPref;
    }

    public void clearNotExistFile(){
        List<VideoBean> list = videoBeanDao.loadAll();
        for(VideoBean videoBean:list){
            if(!new File(videoBean.srcPath).exists()){
                FileUtils.deleteFile(videoBean.thumbnail);
                videoBeanDao.deleteByKey(videoBean.id);
                //FileNameSyncPref.getInstance().insertFileNameSync(videoBean,FileNameSyncPref.FILE_SYNC_ACTION_DELETE);
            }
        }
    }

    public void insertVideoBean(VideoBean videoBean){
        videoBean.id = videoBeanDao.insert(videoBean);
    }

    public boolean existVideoByPath(String path){

        return videoBeanDao.queryBuilder().where(VideoBeanDao.Properties.SrcPath.eq(path)).count()>0;
    }

    public boolean deleteVideoById(long id){

        return true;
    }

    public int deleteVideoByPath(String filePath){

        List<VideoBean> list = videoBeanDao.queryBuilder().where(VideoBeanDao.Properties.SrcPath.eq(filePath)).list();

        for(VideoBean videoBean:list){
            videoBeanDao.delete(videoBean);
        }

        MyLog.D(TAG,"deleteVideoByPath list.size()="+list.size()+",filePath="+filePath);

        return list.size();
    }

    public GetVideo getVideoForHttp(int limit, int offset, int type, int camera){
        GetVideo getVideo = new GetVideo();
        List<VideoBean> list = videoBeanDao.queryBuilder()
                //.where(VideoBeanDao.Properties.VideoType.eq(type))
                .offset(offset)
                .limit(limit)
                .list();
        getVideo.setVideoList(list);
        getVideo.limit = limit;
        getVideo.offset = offset;
        getVideo.count = videoBeanDao.queryBuilder().where(VideoBeanDao.Properties.VideoType.eq(type)).count();

        return getVideo;
    }

    public VideoBean getVideoByPath(String path){
        VideoBean videoBean = null;

        List<VideoBean> list = videoBeanDao.queryBuilder()
                .where(VideoBeanDao.Properties.SrcPath.eq(path))
                .list();

        if(list != null && list.size() > 0){
            videoBean = list.get(0);
        }

        return videoBean;
    }

    public VideoBean getVideoById(long id){
        VideoBean videoBean = null;

        List<VideoBean> list = videoBeanDao.queryBuilder()
                .where(VideoBeanDao.Properties.Id.eq(id))
                .list();

        if(list != null && list.size() > 0){
            videoBean = list.get(0);
        }

        return videoBean;
    }

    public VideoBean getVideoByStringId(String resId){
        VideoBean videoBean = null;

        Long id = null;

        try {
            id = Long.parseLong(resId);
        }catch (Exception e){
            e.printStackTrace();
        }

        if(id == null) return null;

        List<VideoBean> list = videoBeanDao.queryBuilder()
                .where(VideoBeanDao.Properties.Id.eq(id))
                .list();

        if(list != null && list.size() > 0){
            videoBean = list.get(0);
        }

        return videoBean;
    }

    public VideoBean getLastVideo(){
        VideoBean videoBean = null;

        List<VideoBean> list = videoBeanDao.queryBuilder().limit(10).list();

        if(list != null && list.size() > 0){
            videoBean = list.get(3);
        }

        return videoBean;
    }
}
